package cn.yangliu.nacos.comm.tools;

import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import org.springframework.http.MediaType;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * The type Http utils.
 *
 * <p>
 * .
 *
 * @author 问道于盲
 * @date 2019 -12-14
 */
public class WebUtils {

    /**
     * Utility classes, which are collections of static members, are not meant
     * to be instantiated. Even abstract utility classes, which can be extended,
     * should not have public constructors.
     * <p>
     * Java adds an implicit public constructor to every class which does not
     * define at least one explicitly. Hence, at least one non-public
     * constructor should be defined.
     */
    protected WebUtils() {
    }

    /**
     * get http request
     *
     * @return {@link HttpServletRequest}
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes = getRequestAttributes();
        if (servletRequestAttributes == null) {
            return null;
        }
        return servletRequestAttributes.getRequest();
    }

    /**
     * get http response
     *
     * @return {@link HttpServletResponse}
     */
    public static HttpServletResponse getResponse() {
        ServletRequestAttributes servletRequestAttributes = getRequestAttributes();
        if (servletRequestAttributes == null) {
            return null;
        }
        return servletRequestAttributes.getResponse();
    }

    /**
     * get http request session
     *
     * @return {@link HttpSession}
     */
    public static HttpSession getSession() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getSession();
    }

    /**
     * get http request header
     *
     * @param name the name
     * @return {@link String}
     */
    public static String getHeader(String name) {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getHeader(name);
    }

    /**
     * get http request attribute
     *
     * @param name the name
     * @return {@link Object}
     */
    public static Object getAttribute(String name) {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getAttribute(name);
    }

    /**
     * get request attributes
     *
     * @return {@link ServletRequestAttributes}
     */
    public static ServletRequestAttributes getRequestAttributes() {
        return ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes());
    }

    /**
     * get servlet context
     *
     * @return {@link ServletContext}
     */
    public static ServletContext getServletContext() {
        return ContextLoader.getCurrentWebApplicationContext()
                .getServletContext();
    }

    /**
     * Write.
     *
     * @param data the data
     */
    @SneakyThrows
    public static void write(Object data) {
        HttpServletResponse response = getResponse();
        String content;
        if (data instanceof String) {
            content = data.toString();
        }else {
            content = JSON.toJSONString(data);
        }

        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        PrintWriter writer = response.getWriter();
        writer.write(content);
        writer.flush();
        writer.close();
    }
}

